//
//  JHNetWorkTools.swift
//  OC中的单例的封装
//
//  Created by apple on 16/5/26.
//  Copyright © 2016年 apple. All rights reserved.
//

import AFNetworking
//声明枚举类型
enum HTTPMethods:Int {
    case GET
    case POST
}
class JHNetWorkTools: AFHTTPSessionManager {

    static let sharedTool:JHNetWorkTools = {
    let tools = JHNetWorkTools (baseURL: nil)
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
    return tools
    }()
    
/*
tools.GET(<#T##URLString: String##String#>, parameters: <#T##AnyObject?#>, success: <#T##((NSURLSessionDataTask, AnyObject?) -> Void)?##((NSURLSessionDataTask, AnyObject?) -> Void)?##(NSURLSessionDataTask, AnyObject?) -> Void#>, failure: <#T##((NSURLSessionDataTask?, NSError) -> Void)?##((NSURLSessionDataTask?, NSError) -> Void)?##(NSURLSessionDataTask?, NSError) -> Void#>)

*/

        func request(method:HTTPMethods,urlString:String,parameters:AnyObject?,finished:(responseObject:AnyObject?,error:NSError?) -> ()  ){
            
            let success = {(task:NSURLSessionDataTask, result:AnyObject?) -> ()
                in
                finished(responseObject:result ,error:nil)
            }
            let failure = {(task:NSURLSessionDataTask?,error: NSError) -> ()
                in
                finished(responseObject:nil ,error:error)
            }

        if method == .GET {
            GET(urlString, parameters: parameters, success: success, failure: failure)
            
            
//            GET(urlString, parameters: parameters, success: { (_, result) in
//                //_表示忽略
//                //执行请求成功的回调
//                finished(responseObject:result ,error:nil)
//                }, failure: { (_, error) in
//                    //执行失败的回调
//                    finished(responseObject:nil ,error:error)
//            })
//        }else{
//            POST(urlString, parameters: parameters, success: { (_, result) in
//                //_表示忽略
//                //执行请求成功的回调
//                finished(responseObject:result ,error:nil)
//                }, failure: { (_, error) in
//                    //执行失败的回调
//                    finished(responseObject:nil ,error:error)
//            })

        }else{
            POST(urlString, parameters: parameters, success: success, failure: failure)
            }
    
    }
}
